package com.example.add8_17.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.add8_17.R;

public class CircleNumberProgress extends View {

    private final Paint paint;
    private final Paint paint1;
    private final Paint paint2;
    public int mCurrentProgress=50;
    public synchronized void setCurrentProgress(int currentProgress) {
        this.mCurrentProgress = currentProgress;
        invalidate();
    }
    @SuppressLint("ResourceAsColor")
    public CircleNumberProgress(Context context) {
        super(context);
        paint = new Paint();
        paint.setColor(R.color.colorAccent);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(200);
        paint.setStyle(Paint.Style.STROKE);
        paint1 = new Paint();
        paint1.setColor(R.color.colorAccent);
        paint1.setAntiAlias(true);
        paint1.setStrokeWidth(200);
        paint1.setStyle(Paint.Style.STROKE);
        paint2 = new Paint();
        paint2.setTextSize(50);
        paint2.setColor(R.color.colorPrimary);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth()/2;
        canvas.drawCircle(width,width,width-200/2,paint);
        RectF rectF = new RectF(200 / 2, 200 / 2, getWidth() - 200 / 2, getHeight());
        if (mCurrentProgress==0){
            return;
        }
        float percent=(float)mCurrentProgress/100;
        canvas.drawArc(rectF,0,percent*360,false,paint1);
        String textstring=((int)(percent*100))+"%";
        Rect rect = new Rect();
        paint2.getTextBounds(textstring,0,textstring.length(),rect);
        int dy = (paint2.getFontMetricsInt().bottom-paint2.getFontMetricsInt().top)/2-paint2.getFontMetricsInt().bottom;
        int baseLine = getHeight()/2 + dy;
        canvas.drawText(textstring,getWidth()/2-rect.width()/2,baseLine,paint2);
    }
}
